package com.atguigu.common.exception;

import com.atguigu.common.result.ResponseEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @className Assert
 * @Author sofia
 * @Date 2021/9/22
 * @Describe 使用断言简化参数判断
 **/
@Slf4j
public class Assert {
    /**
     * 断言对象不为空
     * 如果对象obj为空，则抛出异常
     * @param obj 待判断对象
     */
    public static void notNull(Object obj, ResponseEnum responseEnum){
        if (obj==null){
            log.info("当前参数为null");
            throw new BusinessException(responseEnum);
        }
    }
    /**
     * 断言对象为空
     * 如果对象obj不为空，则抛出异常
     * @param object
     * @param responseEnum
     */
    public static void isNull(Object object, ResponseEnum responseEnum){
        if (object != null){
            log.info("对象不为空");
            throw new BusinessException(responseEnum);
        }
    }
    /**
     * 断言表达式为真
     * 如果不为真，则抛出异常
     *
     * @param expression 是否成功
     */
    public static void isTrue(Boolean expression, ResponseEnum responseEnum){
        if (!expression){
            log.info("表达式不为true");
            throw new BusinessException(responseEnum);
        }
    }
    /**
     * 断言两个对象不相等
     * 如果相等，则抛出异常
     * @param object1
     * @param object2
     * @param responseEnum
     */
    public static void notEquals(Object object1, Object object2, ResponseEnum responseEnum){
        if (object1.equals(object2)){
            log.info("两个对象相等");
            throw new BusinessException(responseEnum);
        }
    }
    /**
     * 断言两个对象相等
     * 如果不相等，则抛出异常
     * @param object1
     * @param object2
     * @param responseEnum
     */
    public static void isEquals(Object object1, Object object2, ResponseEnum responseEnum){
        if (!object1.equals(object2)){
            log.info("两个对象不相等");
            throw new BusinessException(responseEnum);
        }
    }
    /**
     * 断言参数不为空
     * 如果为空，则抛出异常
     * @param parm
     * @param responseEnum
     */
    public static void isNotEmpty(String parm,ResponseEnum responseEnum){
        if (parm.isEmpty()){
            log.info("参数为空");
            throw new BusinessException(responseEnum);
        }
    }
}
